package demo.game.听心算;

import com.titan.core.utils.data.RandomUtils;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.utils.thread.ThreadUtils;
import com.titan.services.windows.ActiveX.v1.component.SapiSpVoiceComponent;

public class 十以内加减法 {
    private final static SapiSpVoiceComponent SAP = new SapiSpVoiceComponent();
    private final static int COUNT = 10;
    private final static int SLEEP = 500;
    
    public static void main(String[] args) {
        try {
            while(true) {
                run();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void run() throws Exception {
        // 初始数值
        SAP.speak("清盘");
        int result = RandomUtils.random(1, 9);
        ConsoleUtils.log(result);
        SAP.speak("初始" + result);
        ThreadUtils.sleep(SLEEP);
        
        // 加减法
        for (int i = 1; i < COUNT; i++) {
            int temp = RandomUtils.random(1, 9);
            int symbol = RandomUtils.randomToIntegerBySelect(-1, 1);
            String text = null;
            if(symbol==1) {
                text = "加" + temp;
                result = result + temp;
            } else {
                if(result<temp) {
                    // 随机生成减数太大，不能小于0，所以减数改为加数
                    text = "加" + temp;
                    result = result + temp;
                } else {
                    text = "减" + temp;
                    result = result - temp;
                }
            }
            ConsoleUtils.log(text);
            SAP.speak(text);
            ThreadUtils.sleep(SLEEP);
        }
        
        // 公布结果
        ThreadUtils.sleep(2000);
        ConsoleUtils.log("结果", result);
        SAP.speak("结果"+result);
    }
}
